#include <stdio.h>
#include <string.h>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

#define MAXLEN 64

int main(void)
{
    FILE* ReturnMatrixfile;
    ReturnMatrixfile = fopen("test1.txt", "r");
    // ReturnMatrixfile = fopen("ReturnMatrix.txt", "r");
    int participantA, participantB;

    // 获取参与者分别的对策数量和对策名称
    fscanf(ReturnMatrixfile, "%d %d", &participantA, &participantB);//策略数
    int strategyNum = participantA * participantB;
    char** optionA = (char **)malloc(participantA * sizeof(char*));
    for(int i = 0; i < participantA; i++){
        optionA[i] = (char *)malloc(MAXLEN * sizeof(char));
    }
    char** optionB = (char **)malloc(participantB * sizeof(char*));
    for(int i = 0; i < participantB; i++){
        optionB[i] = (char *)malloc(MAXLEN * sizeof(char));
    }
    char * tmp = (char*)malloc(MAXLEN * sizeof(char));
    fgets(tmp, MAXLEN, ReturnMatrixfile);       // 把第一行的回车给读了
    free(tmp);
    tmp = NULL;
    for(int i = 0; i < participantA; i++){  //A策略名
        fgets(optionA[i], MAXLEN, ReturnMatrixfile);
        optionA[i][strlen(optionA[i]) - 1] = 0;
        // printf("%s\n", optionA[i]);
        // printf("%d\n", strlen(optionA[i]));
    }
    for(int i = 0; i < participantB; i++){  //B策略名
        fgets(optionB[i], MAXLEN, ReturnMatrixfile);
        optionB[i][strlen(optionB[i]) - 1] = 0;
        // printf("%s\n", optionB[i]);
        // printf("%d\n", strlen(optionB[i]));
    }

    // 创建收益矩阵
    pair<int, int>* ReturnMatrix;
    size_t ReturnMatrixLen = strategyNum * sizeof(struct pair<int, int>);
    ReturnMatrix = (pair<int, int> *)malloc(ReturnMatrixLen);
    memset(ReturnMatrix, 0, ReturnMatrixLen);

    // 读取收益矩阵
    for(int i = 0; i < strategyNum; i++){
        struct pair<int, int> tmpp;
        fscanf(ReturnMatrixfile, "%d %d", &tmpp.first, &tmpp.second);
        ReturnMatrix[i] = tmpp;
    }

    // 计算占优策略
    printf ("============================="
            "\n计算占优策略中...\n");
    // 先选出参与者B选择策略1时对应的最佳策略
    struct pair<int, int>tmpp = ReturnMatrix[0];
    int choice = 0;
    for(int i = 0; i < participantA; i++){
        if(ReturnMatrix[i*participantB].first > tmpp.first){
            choice = i;
            tmpp = ReturnMatrix[i*participantB];
        }
    }
    bool flagA = true;
    for(int choiceB = 1; choiceB < participantB; choiceB++){
        for(int choiceA = 0; choiceA < participantA; choiceA++){
            if(ReturnMatrix[choiceA * participantB + choiceB].first > tmpp.first){
                flagA = false;
                break;
            }
        }
        if(flagA == false){
            printf("参与者A不存在占优策略\n");
            break;
        }
    }
    if(flagA){
        printf("参与者A的占优策略: %s\n", optionA[choice]);
    }
    // 先选出参与者A选择策略1时对应的最佳策略
    tmpp = ReturnMatrix[0];
    choice = 0;
    for(int i = 0; i < participantB; i++){
        if(ReturnMatrix[i].second > tmpp.second){
            choice = i;
            tmpp = ReturnMatrix[i];
        }
    }
    bool flagB = true;
    for(int choiceA = 1; choiceA < participantA; choiceA++){
        for(int choiceB = 0; choiceB < participantB; choiceB++){
            if(ReturnMatrix[choiceA * participantB + choiceB].second > tmpp.second){
                flagB = false;
                break;
            }
        }
        if(flagB == false){
            printf("参与者B不存在占优策略\n");
            break;
        }
    }
    if(flagB){
        printf("参与者B的占优策略: %s\n", optionB[choice]);
    }
    printf("计算结束");

    // 计算纯策略结果
    int sum = 0;
    printf ("\n============================="
            "\n计算纯策略纳什均衡策略中...\n");
    for(int choiceA = 0; choiceA < participantA; choiceA++){
        for(int choiceB = 0; choiceB < participantB; choiceB++){
            struct pair<int, int> tmpp;
            tmpp = ReturnMatrix[choiceA * participantB + choiceB];
            bool flagB = true;
            for(int i = 0; i < participantB; i++){
                if(ReturnMatrix[choiceA * participantB + i].second > tmpp.second){
                    flagB = false;
                }
            }
            bool flagA = true;
            for(int i = 0; i < participantB; i++){
                if(ReturnMatrix[i * participantB + choiceB].first > tmpp.first){
                    flagA = false;
                }
            }
            if(flagA && flagB){
                printf("(%s, %s)\n", optionA[choiceA], optionB[choiceB]);
                printf("(%d, %d)\n", choiceA + 1, choiceB + 1);
                sum++;
            }
        }
    }
    
    if(sum == 0){
        printf("计算结束\n不存在纯策略纳什均衡策略...");
    }else {
        printf("计算结束");
    }
    return 0;
}